const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const session = require('express-session')
const cookieParser = require('cookie-parser')
// const MemcachedStore = require("connect-memcached")(session)

const app = express()

app.all('*',function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With');
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
    if (req.method == 'OPTIONS') {
        res.send(200);
    }
    else {
        next();
    }
});

const router = require('./router/router.js')

app.use(bodyParser.urlencoded({ extended: false, limit: '20000kb'}))
app.use(bodyParser.json({"limit": "20000kb"}))

app.use(express.static(path.join(__dirname, '../client/dist')))

//设置访问得到后缀名名字为 html
app.engine('html', require('express-art-template'))
app.set('views', '../client/dist')


app.use(cookieParser('hj'))


app.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: true,
    cookie:{
        maxAge: 1000*60*10
    }
}))

//当有请求过来的时候 会刷新session 注意此时是在使用session之后
app.use(function(req, res, next){
    req.session._garbage = Date()
    req.session.touch()
    next()
})

app.use(router)


app.listen(3003, function(){
    console.log('server is running...')
})